<template>
    <div>
        <div style="margin-top: 8px;display: flex;justify-content: center">
            <el-input v-model="keyWords" placeholder="请输入用户名进行搜索" clearable
                      prefix-icon="el-icon-search" style="width: 400px;margin-right: 8px" @keydown.enter.native="doSearch"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="doSearch">搜索</el-button>
        </div>
        <div class="hr-container">
            <el-card class="hr-card" v-for="(hr,index) in hrs" :key="index">
                <div class="clearfix">
                    <div slot="header" style="display: flex;align-items: center">
                        <img :src="hr.userface" :alt="hr.name" :title="hr.name" class="userfaceImg">
                        <div style="margin-left: 8px;font-size: 20px">{{hr.name}}</div>
                    </div>
                    <div>
                        <el-button style="padding: 3px 0;color: red;"
                                   type="text" icon="el-icon-delete" @click="deleteRole(hr)">删除</el-button>
                    </div>
                </div>
                <div class="userInfo-container">

                    <div>手机号码：{{hr.phone}}</div>
                    <div>电话名：{{hr.telephone}}</div>
                    <div>地址：{{hr.address}}</div>
                    <div>用户状态：<el-switch
                            v-model="hr.enabled"
                            @change="enabledChange(hr)"
                            active-text="开启"
                            inactive-text="禁用">
                    </el-switch></div>
                    <div>用户角色：
                        <el-tag type="success" style="margin-right: 5px" v-for="(role,indexr) in hr.roles" :key="indexr">{{role.nameZh}}</el-tag>
                        <el-popover
                                @show="showSelect(hr)"
                                @hide="hideSelect(hr)"
                                placement="right"
                                title="角色列表"
                                width="200"
                                trigger="click"
                                >
                            <el-select v-model="selectRoles" multiple placeholder="请选择">
                                <el-option
                                        v-for="(role,indexk) in allRoles"
                                        :key="indexk"
                                        :label="role.nameZh"
                                        :value="role.id">
                                </el-option>
                            </el-select>
                            <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
                        </el-popover>
                    </div>
                    <div>备注：{{hr.remark}}</div>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>
    export default {
        name: "SysHr",
        methods:{
            deleteRole(hr){
                this.$confirm('此操作将删除HR【'+hr.name+'】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(hr){
                        this.deleteRequest("/system/hr/"+hr.id).then(resp=>{
                            if(resp){
                                this.initHrs();
                            }
                        })
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doSearch(){
                this.initHrs();
            },
            hideSelect(hr){
                let roles = hr.roles;
                let flag = !roles.every(role => this.selectRoles.includes(role.id)) || roles.length !== this.selectRoles.length;
                if(flag){
                    let url = "/system/hr/role?hrid="+hr.id;
                    this.selectRoles.forEach(r=>{
                        url += "&rids=" + r;
                    });
                    this.putRequest(url).then(resp=>{
                        if(resp){
                            this.initHrs();
                        }
                    })
                }
            },
            showSelect(hr){
                this.initAllRoles();
                this.selectRoles=[];
                let roles = hr.roles;
                roles.forEach(r=>{
                    this.selectRoles.push(r.id);
                })
            },
            initAllRoles(){
                this.getRequest("/system/hr/roles").then(resp=>{
                    if(resp){
                        this.allRoles = resp;
                    }
                })
            },
            enabledChange(hr){
                delete hr.roles;
                this.putRequest("/system/hr/",hr).then(resp=>{
                    if(resp){
                        this.initHrs();
                    }
                })
            },
            initHrs(){
                this.getRequest("/system/hr/?keyWord="+this.keyWords).then(resp=>{
                    if(resp){
                        this.hrs = resp;
                    }
                })
            }
        },
        data(){
            return {
                keyWords:"",
                hrs:[],
                allRoles:[],
                selectRoles:[]
            }
        },
        mounted() {
            this.initHrs();
        }
    }
</script>

<style>
    .userInfo-container{
        font-size: 12px;
        font-weight: bold;
        color: #409eff;
    }
    .userfaceImg {
        width: 60px;
        height: 60px;
        border-radius: 60px;
        margin-left: 10px;
    }
    .hr-container {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }
    .hr-card {
        width: 500px;
        margin-bottom: 10px;
    }
    .clearfix {
        display: flex;
        margin-bottom: 10px;
        justify-content: space-between;
        align-items: center;
    }

</style>